package com.A21.GuoSai.demo.service_entity.test;

import io.camunda.zeebe.model.bpmn.Bpmn;
import io.camunda.zeebe.model.bpmn.BpmnModelInstance;
import io.camunda.zeebe.model.bpmn.instance.ExclusiveGateway;
import io.camunda.zeebe.model.bpmn.instance.FlowNode;
import io.camunda.zeebe.model.bpmn.instance.Process;
import io.camunda.zeebe.model.bpmn.instance.ServiceTask;
import org.junit.jupiter.api.Test;

import javax.xml.stream.XMLStreamException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

//@ZeebeSpringTest
public class TryToGetData {

    public static String getBpmnString() throws IOException {
        String path="processes/purchase.bpmn";
        InputStream resourceAsStream = TryToGetData.class.getClassLoader().getResourceAsStream(path);
        BufferedReader reader=new BufferedReader(new InputStreamReader(resourceAsStream));
        String ss;
        String bpmn="";
        while ((ss=reader.readLine()) != null) {
            bpmn = bpmn + ss+"\n";
        }

        return bpmn;
    }

    @Test
    public void getBpmn() throws IOException {
        String path="processes/purchase_zh_CN.bpmn";
        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream(path);
        BufferedReader reader=new BufferedReader(new InputStreamReader(resourceAsStream));
        String ss;
        String bpmn="";
        while ((ss=reader.readLine()) != null) {
            bpmn = bpmn + ss+"\n";
        }
        System.out.println(bpmn);
    }

    @Test
    public void getBpmnPicture() throws IOException, XMLStreamException {
        /*String bpmn=getBpmnString();
        //字符串需要转成输入流，ZeebeDeployment的内部原理也是先将字符串转化为字节
        InputStream inputStream = new ByteArrayInputStream(bpmn.getBytes());
        // 创建转换对象
        BpmnXMLConverter converter = new BpmnXMLConverter();

        // 创建XMLStreamReader读取XML资源
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLStreamReader reader = factory.createXMLStreamReader(inputStream);

        // 把XML转换成BpmnModel对象
        BpmnModel bpmnModel = converter.convertToBpmnModel(reader);

// 使用默认配置获得流程图表生成器，并生成追踪图片字符流
        DefaultProcessDiagramGenerator processDiagramGenerator = new DefaultProcessDiagramGenerator();
        InputStream imageStream = processDiagramGenerator.generateDiagram(bpmnModel, "jpg",  "宋体", "微软雅黑", "Arial", null, 30.0);
//        BufferedImage imageStream=processDiagramGenerator.generatePngImage(bpmnModel,200.0);

        File file=new File("D:\\Typora\\Typora_Work\\Zeebe\\Zeebe_bpmn_pic\\002.jpg");
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
        byte[] b=new byte[10240];
        int size;
        while ((size=imageStream.read(b))!=-1) {
            bufferedOutputStream.write(b);
        }*/
//        ImageIO.write(imageStream,"002.png",file);
    }

    @Test
    public void testZeebe01() {
        InputStream input=this.getClass().getClassLoader().getResourceAsStream("processes/my.bpmn");
        BpmnModelInstance modelInstance= Bpmn.readModelFromStream(input);
        System.out.println(modelInstance.getModelElementsByType(Process.class));

//        for(Process process:modelInstance.getModelElementsByType(Process.class))
//            System.out.println(process.getId());
//
//        for(ConditionExpression expression:modelInstance.getModelElementsByType(ConditionExpression.class)){
//            System.out.println(expression.getRawTextContent());
//        }
//
//        for(ServiceTask task: modelInstance.getModelElementsByType(ServiceTask.class)){
//            task.getSucceedingNodes();
//        }

        for(ExclusiveGateway exclusiveGateway:modelInstance.getModelElementsByType(ExclusiveGateway.class)){
            List<FlowNode> list = exclusiveGateway.getPreviousNodes().list();
            int i=0;
            for (FlowNode flowNode : list){
                if(flowNode.getElementType().getInstanceType()==ServiceTask.class){
                    System.out.println(exclusiveGateway.getName()+"的前一个serviceTask为"+flowNode.getName());
                    i=1;
                    break;
                }
            }
            if(i==0){
                System.out.println(exclusiveGateway.getName()+"的前面没有serviceTask");
            }
        }

        // 获取ServiceTask类型的组件
//        Collection<EndEvent> tasks=modelInstance.getModelElementsByType(EndEvent.class);
//        for(EndEvent task : tasks){
//            System.out.print("名称:"+task.getName()+",ID:"+task.getId()+",");
//            Query<FlowNode> previousNodes = task.getPreviousNodes();
//            List<FlowNode> list=previousNodes.list();
//            System.out.println();
//            System.out.print("前趋结点为: ");
//            for(FlowNode flowNode:list){
//                System.out.print(flowNode.getId());
//            }
//            System.out.println();
//        }
    }


}
